﻿//by  张海军 710605420@qq.com
#if HOTFIX_ENABLE
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using XLua;

using LazyFramework.LUA.UI;

namespace LazyFramework.UI
{
    public class UILuaViewControllerBase : UILuaViewBase
    {
        public string viewClassName = string.Empty;
#region LuaHandler
        internal LuaFunction _lua_OnInitialize;
        internal LuaFunction _lua_Update;
        internal LuaFunction _lua_FixedUpdate;
        internal LuaFunction _lua_OnOpen;
        internal LuaFunction _lua_OnClose;
        internal LuaFunction _lua_OnRelease;
        internal LuaFunction _lua_GetUIType;
        internal LuaFunction _lua_GetUILayer;

#endregion


        public LuaTable luaUI;

        /// <summary>
        /// 根据 viewClassName 加载对应的 LuaFunction 并 call it 产生一个 table 实例
        /// </summary>
        /// <returns></returns>
        public void LoadLuaCode()
        {
            if (!LuaUIManager.Instance.luaUINames.ContainsKey(viewClassName))
            {
                Debug.LogErrorFormat("Can not find LuaUI[{0}], pls check!", viewClassName);
                return ;
            }

            luaUI = XluaManager.Instance.createLuaUIFunc.Func<string, UILuaViewControllerBase, LuaTable>(viewClassName, this);
            if (luaUI == null)
            {
                Debug.LogErrorFormat("Get LuaTable fail when load LuaUI[{0}], pls check lua file!", viewClassName);
                return ;
            }

            SetupCallbacks();

            LuaUIManager.Instance.AddLuaUI(this);
            return ;
        }

         /// <summary>
        /// 设置 Lua 回调
        /// </summary>
        /// <returns></returns>
        private void SetupCallbacks() {
            if (luaUI == null)
                return ;
            luaUI.SetMetaTable(LazyFramework.XluaManager.Instance.globalMT);
            luaUI.Get("OnInitialize", out _lua_OnInitialize);
            luaUI.Get("OnOpen",                     out _lua_OnOpen);
            luaUI.Get("Update",                     out _lua_Update);
            luaUI.Get("FixedUpdate",                out _lua_FixedUpdate);
            luaUI.Get("OnClose",                    out _lua_OnClose);
            luaUI.Get("OnRelease",                  out _lua_OnRelease);
            luaUI.Get("GetUIType",                  out _lua_GetUIType);
            luaUI.Get("GetUILayer",                 out _lua_GetUILayer);
            return ;
        }


        /// <summary>
        /// 置空所有回调
        /// </summary>
        /// <remarks>请务必置空，否则会报 "try to dispose a LuaEnv with C# callback" 错误</remarks>
        private void RemoveCallbacks() {
            if (_lua_OnInitialize != null) _lua_OnInitialize.Dispose();
            if (_lua_OnOpen != null) _lua_OnOpen.Dispose();
            if (_lua_Update != null) _lua_Update.Dispose();
            if (_lua_FixedUpdate != null) _lua_FixedUpdate.Dispose();
            if (_lua_OnClose != null) _lua_OnClose.Dispose();
            if (_lua_OnRelease != null) _lua_OnRelease.Dispose();
            if (_lua_GetUILayer != null) _lua_GetUILayer.Dispose();

            _lua_OnInitialize = null;
            _lua_OnOpen                     = null;
            _lua_Update                     = null;
            _lua_FixedUpdate                = null;
            _lua_OnClose                    = null;
            _lua_OnRelease                  = null;
            _lua_GetUIType                  = null;
            _lua_GetUILayer                 = null;
        }


        protected override void OnInitialize()
        {
            base.OnInitialize();
            LoadLuaCode();
            luaUI.Set("gameObject", Actor);
            luaUI.Set("transform", Actor.transform);
            luaUI.Set("self", this);

            if (_lua_OnInitialize != null)
                _lua_OnInitialize.Func<LuaTable,LuaTable>(luaUI);
        }

        protected override void OnOpen()
        {
            base.OnOpen();
            if (_lua_OnOpen != null)
                _lua_OnOpen.Func<LuaTable, LuaTable>(luaUI);
        }

        protected override void OnClose()
        {
            base.OnClose();
            if (_lua_OnClose != null)
                _lua_OnClose.Func<LuaTable, LuaTable>(luaUI);
        }

        protected override void OnRelease()
        {
            base.OnRelease();
            if (_lua_OnRelease != null)
                _lua_OnRelease.Func<LuaTable, LuaTable>(luaUI);
        }
        public override UILayer GetUILayer()
        {
            if (_lua_GetUILayer != null)
                _lua_GetUILayer.Func<LuaTable, UILayer>(luaUI);
            return base.GetUILayer();
        }

        public override UIType GetUIType()
        {
            if (_lua_GetUIType != null)
                _lua_GetUIType.Func<LuaTable,UIType>(luaUI);
            return base.GetUIType();
        }
    }

}
#endif